// pages/regimental-commander/confirm-receipt/confirm-receipt.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploadFileUrl: 'upload.uploadPic',
    fileList: [],
    order: null,
    btn: null
  },

  /**
   * 图片选中上传(多图)
   * @param {Object} e 
   */
  afterRead: function(e) {
    const { file = [] } = e.detail
    let promiseArray = []
    const uploaderUrl = app.getNetAddresss(this.data.uploadFileUrl)
    wx.showLoading({
      title: '上传中',
    })
    file.forEach(item => {
      promiseArray.push(new Promise((resolve, reject) => {
        wx.uploadFile({
          filePath: item.url,
          name: 'file',
          url: uploaderUrl,
          success: function(res) {
            resolve(res)
          },
          fail: function(err) {
            reject(err)
          }
        })
      }))
    })
    Promise.all(promiseArray).then(resArray => {
      wx.hideLoading()
      const addUrlArray = resArray.map(item => {
        const resData = JSON.parse(item.data)
        return {url: resData.data.img_url}
      })
      const currentImgUrls = [...this.data.fileList, ...addUrlArray]
      this.setData({
        fileList: currentImgUrls
      })
    }).catch(err => {
      wx.hideLoading()
    })
  },

  /**
   * 删除已上传的图片
   * @param {Object} e 
   */
  deleteFile: function(e) {
    const { index } = e.detail
    let currentData = this.data.fileList
    currentData.splice(index, 1)
    this.setData({
      fileList: currentData
    })
  },

  submitConfirm: function() {
    const { fileList, order, btn } = this.data
    if(!fileList.length) {
      wx.showToast({
        icon: 'none',
        title: '请上传商品图片',
      })
      return
    }
    const imgArr = fileList.map(item => item.url)
    const params = {
      order_refund_id: order.id,
      images: imgArr
    }
    const url = app.getNetAddresss(btn.api)
    console.log(params, url)
    wx.showLoading({
      title: '数据提交中...',
    })
    app._postNetWork({
      url,
      data: params,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          wx.showModal({
            title: '成功',
            content: resData.msg,
            showCancel: false,
            confirmText: '好的',
            confirmColor: '#ee0a24',
            complete: (res) => {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/regimental-commander/order/order?orderType=waitRefund',
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: resData.msg,
          })
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const params = JSON.parse(decodeURIComponent(options.params))
    this.setData({
      order: params.order,
      btn: params.btn
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})